
const pending = 'PENDING'
const resolved = 'RESOLVED'
const rejected = 'REJECTED'
const isFunction = (fn) => typeof fn == "function"
//在执行的时候 会改变状态
class MyPromise {
    constructor(executor) {
        this.thenCallbackQueue = [];
        this.catchCallbackQueue = [];
        this._status = pending
        this._value = undefined
        executor(this._resolve.bind(this), this._reject.bind(this));
    }

    _resolve(val) {

        if (this._status !== pending) return
        const run = () => {
            this._status = resolved

            this.thenCallbackQueue.forEach(el => el(val))
            // console.log(this.thenCallbackQueue,"====");
            // // while (cd == this.thenCallbackQueue.shift()) {
            // //     this._value = val
            // //     cd(123)
            // //     console.log(cd,"====");
            // // }
        }

        setTimeout(run, 0);
    }
    _reject(err) {
        if (this._status !== pending) return
        const run = () => {
            this._status = rejected
            this._value = err
            if (this.catchCallbackQueue.length == 0) {
                throw new Error(value)
            }
            let cd
            while (cd = this.catchCallbackQueue.shift()) {
                cd(err)
            }
        }
        setTimeout(run, 0);
    }
    //.then的过程是把 then_cd加入执行队列
    then(then_cd, reject_cd) {
        const { _status, _value } = this
        return new MyPromise((resolved_next, reject_next) => {
            const _resolveed = (value) => {
               let res =  then_cd(value)
               if(res instanceof MyPromise){
                 res.then(resolved_next,reject_next)
               }
               resolved_next(res)
            //   try {
            //     if(!isFunction(then_cd)){
            //         resolved_next(value) 
            //     }else{
            //         const res = then_cd(value)
            //         console.log(res,"====res");
            //         console.log(res instanceof MyPromise,"=====原型");
            //         if(res instanceof MyPromise){
            //             res.then(resolved_next,reject_next)
            //         }else{
            //            resolved_next(res)
            //         }
            //     }
            //   } catch (error) {
                 
            //   }
            }
            const _rejected = (value) => {
             try {
                if(!isFunction(reject_cd)){
                   reject_next(value)   
                }else{
                    const res = reject_cd(value)
                    
                    if(res instanceof MyPromise){
                         res.then(resolved_next,reject_next)
                    }else{
                        resolved_next()
                    }
                }
             } catch (error) {
                
             }
            }
            switch (_status) {
                case pending:
                    this.thenCallbackQueue.push(_resolveed)
                    this.catchCallbackQueue.push(_rejected)
                    break;
                case resolved:
                    resolved_next(_value)
                case resolved:
                    reject_next(_value)
                default:
                    break;
            }
        })


    }
    catch(reject_cd) {
        this.then(null, reject_cd)
    }
}
